Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
Is there a way I can make the input to act like "display: inline-block"?

What I am trying to do is implement a simple inline editing, I'm using a normal div to display the text when the edit state is false, and render an input when the edit state is true.

The problem I'm facing is that the input doesn't wrap around the text content it's got, like a div with display: inline-block would. I tried setting the size attribute of the input to be that of the current value's length, but it's not wrapping around the content perfectly.

Any ideas how to do it? I guess contentEditable on the span itself, without an input, is an option, but I want to be able to select all the content in the input when it gets the focus.

Here is the code.

import { useState, useRef, useEffect } from "react";

export default function App() {
  const [value, setValue] = useState("test");
  const [active, setActive] = useState(false);
  const ref = useRef();

  useEffect(() => {
    ref.current?.select();
  }, [active]);

  const length = value.split(" ").join("").length;

  return (
    <>
      {!active && <span onClick={() => setActive(true)}>{value}</span>}
      {active && (
        <input
          value={value}
          size={length}
          ref={ref}
          onChange={(e) => setValue(e.target.value)}
          onBlur={() => setActive(false)}
        />
      )}
    </>
  );
}

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda